package cc.fs.androidcode.ui.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import cc.fs.androidcode.utils.L;

/**
 * Created by fostion on 2015/12/9.
 *                     测量       摆放      绘制
 * 学习view的绘制流程 measure -> layout -> draw
 */
public class MyGroupView extends RelativeLayout {

    private int screenWidth = 0;
    private int screenHeight = 0;

    public MyGroupView(Context context) {
        super(context);
        init();
    }

    public MyGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected void init() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
//        screenWidth = dm.widthPixels;
//        screenHeight = dm.heightPixels;

    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        L.e("-----group onMeasure------- widthMeasureSpec:" + widthMeasureSpec + "   heightMeasureSpec:" + heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        screenWidth = getMeasuredWidth();
        screenHeight = getMeasuredHeight();
        //计算每个子view的大小
        int mWidthMeasure = 0;
        int mHeightMeasure = 0;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = this.getChildAt(i);
            mWidthMeasure += child.getMeasuredWidth();
            mHeightMeasure += child.getMeasuredHeight();
        }
        //设置width和hegiht getDefaultSize是用来
        setMeasuredDimension(getDefaultSize(mWidthMeasure, widthMeasureSpec), getDefaultSize(mHeightMeasure, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        L.e("-----group onLayout-------");
        super.onLayout(changed, l, t, r, b);
        //控制子view的摆放顺序
        layoutVertial();
    }

    boolean isAdd = true;

    /**
     * 控制子View的摆放顺序
     */
    protected void layoutVertial() {

        int totalWidth = 0;
        int totalHeight = 0;
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {

            final View child = getChildAt(i);
            if (child != null) {
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();

                Log.e("-------------------- ", "  " + (totalWidth + childWidth));

                if ((totalWidth + childWidth) >= screenWidth) {
                    isAdd = false;
                }

                if ((totalWidth - childWidth) < 0) {
                    isAdd = true;
                }

                if (isAdd) {
                    child.layout(totalWidth, totalHeight, totalWidth + childWidth, totalHeight + childHeight);
                    totalWidth += childWidth;
                    totalHeight += childHeight;
                } else {
                    totalWidth -= childWidth;
                    totalHeight += childHeight;
                    child.layout(totalWidth, totalHeight, totalWidth + childWidth, totalHeight + childHeight);
                }




//                int endPos = totalWidth + childWidth;
//                Log.e(" ---屏幕宽  " + screenWidth + "  --- 当前开始位置 " + totalWidth, "-------view的宽度 " + childWidth + "  ---结束位置  " + endPos + "     第几个 " + (i + 1) + "  状态 " + isAdd);
            }
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        L.e("-----group onDraw-------");
        super.onDraw(canvas);
    }
}
